package com.server.llh.llhserverwork.utils;


import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by ZTF on 2017-06-03.
 * 银行卡  手机号  身份证号隐藏加星
 */

public class GoneNumberUtil {
    /**
     * 检查是否是电话号码
     * @return
     */
    public static boolean isMobileNum(String mobiles) {
        Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$");
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }

    /**
     * 手机号中间四位不显示
     * @param mobiles
     * @return
     */
    public static String isMobileGoneNum(String mobiles) {
        if (isMobileNum(mobiles)==true){
            return mobiles.substring(0,3)+"****"+mobiles.substring(7,mobiles.length());
        }else{
            return "telephone is error";
        }
    }

    /**
     * 验证是否是身份证号
     * @param text
     * @return
     */
    public static boolean personIdValidation(String text) {
        String regx = "[0-9]{17}[Xx]";
        String reg1 = "[0-9]{15}";
        String regex = "[0-9]{18}";
        return text.matches(regx) || text.matches(reg1) || text.matches(regex);
    }
    /**
     * 身份证号 隐藏 年月日  从第9-16
     */
    public static String HideIdentity(String number) {
        if (personIdValidation(number)==true){
            StringBuilder sb = new StringBuilder();
            if (number!=null && number.length() > 16) {
                for (int i = 0; i < number.length(); i++) {
                    char c = number.charAt(i);
                    if (i >= 8 && i <= 15) {
                        sb.append('*');
                    } else {
                        sb.append(c);
                    }
                }
            }
            return sb.toString();
        }else{
            return "icCard is error";
        }

    }

    /**
     * 校验银行卡卡号
     * @param cardId
     * @return
     */
    public static boolean checkBankCard(String cardId) {
        char bit = getBankCardCheckCode(cardId.substring(0, cardId.length() - 1));
        if(bit == 'N'){
            return false;
        }
        return cardId.charAt(cardId.length() - 1) == bit;
    }

    /**
     * 从不含校验位的银行卡卡号采用 Luhm 校验算法获得校验位
     * @param nonCheckCodeCardId
     * @return
     */
    public static char getBankCardCheckCode(String nonCheckCodeCardId){
        if(nonCheckCodeCardId == null || nonCheckCodeCardId.trim().length() == 0
                || !nonCheckCodeCardId.matches("\\d+")) {
            //如果传的不是数据返回N
            return 'N';
        }
        char[] chs = nonCheckCodeCardId.trim().toCharArray();
        int luhmSum = 0;
        for(int i = chs.length - 1, j = 0; i >= 0; i--, j++) {
            int k = chs[i] - '0';
            if(j % 2 == 0) {
                k *= 2;
                k = k / 10 + k % 10;
            }
            luhmSum += k;
        }
        return (luhmSum % 10 == 0) ? '0' : (char)((10 - luhmSum % 10) + '0');
    }

    /**
     * 银行卡号隐藏
     * @param bankAccount
     * @return
     */
    public static  String chackBankCard(String bankAccount){
        if (checkBankCard(bankAccount)==true){
            int length=bankAccount.length();
            String bankAccountStr=bankAccount.substring(0, length-15)+"*******"+bankAccount.substring(length-4);
            return bankAccountStr.toString();
        }else{
            return "bank card is error";
        }

    }
}
